package com.newear.util;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

import java.util.List;

/**
 * Created by Administrator on 2018/5/31 0031.
 */

public class WifiManagerUtil {

    public interface WifiCipherType {
        int WIFICIPHER_WPA = 1;
        int WIFICIPHER_WEP = 2;
        int WIFICIPHER_NOPASS = 3;
    }
    private WifiManager wifiManager;
    private volatile static WifiManagerUtil instance;
    private WifiManagerUtil(Context context) {
        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }

    public static WifiManagerUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (WifiManagerUtil.class) {
                //当第一次调用getInstance方法时，即instance为空时，同步操作，保证多线程实例唯一
                //当以后调用getInstance方法时，即instance不为空时，不进入同步代码块，减少了不必要的同步开销
                if (instance == null) {
                    instance = new WifiManagerUtil(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    /**
     * 开启wifi
     */
    public void startWifi() {
        if (!wifiManager.isWifiEnabled()) {
            //开启wifi
            wifiManager.setWifiEnabled(true);
        }
    }

    /**
     * 扫描wifi
     */
    public void search() {
        startWifi();
        wifiManager.startScan();
    }

    public WifiManager getWifiManager() {
        return wifiManager;
    }

    /**
     * 判断是否已经存在连接信息
     * @param SSID
     * @return
     */
    public WifiConfiguration isExsits(String SSID) {
        List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration existingConfig : existingConfigs) {
            if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
                return existingConfig;
            }
        }
        return null;
    }

    /**
     * 连接wifi
     * @param config
     */
    public void connect(WifiConfiguration config) {
        int wcgID = wifiManager.addNetwork(config);
        wifiManager.enableNetwork(wcgID, true);
    }

    /**
     * 创建一个连接wifi的Config
     * @param ssid
     * @param pwd
     * @param type
     */
    public WifiConfiguration createWifiConfig(String ssid, String pwd, int type) {
        WifiConfiguration configuration = null;
        if (type == WifiCipherType.WIFICIPHER_WPA) {
            configuration = new WifiConfiguration();
            configuration.SSID = "\"" + ssid + "\"";
            configuration.preSharedKey = "\"" + pwd + "\"";
            configuration.hiddenSSID = true;
            configuration.status = WifiConfiguration.Status.ENABLED;
            configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            configuration.allowedPairwiseCiphers
                    .set(WifiConfiguration.PairwiseCipher.TKIP);
            configuration.allowedPairwiseCiphers
                    .set(WifiConfiguration.PairwiseCipher.CCMP);
            configuration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

        } else if (type == WifiCipherType.WIFICIPHER_WEP){
            configuration = new WifiConfiguration();
            configuration.hiddenSSID = true;
            configuration.wepKeys[0]= "\""+pwd+"\"";
            configuration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
            configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        }
        return configuration;
    }

}
